function disabledForm(form, disabled)
{
	if(disabled)
	{
		form.setAttribute("disabled", 1);
	}
	else
	{
		form.removeAttribute("disabled")
	}
	for(var i=0; i<form.elements.length; i++)
	{
		form.elements[i].disabled = disabled
	}
}

function clearForm(form)
{
	for(var i=0; i<form.elements.length; i++)
	{
		if(form.elements[i].type.toUpperCase() != "SUBMIT" && form.elements[i].type.toUpperCase() != "BUTTON")
		{
			form.elements[i].value = "";
			form.elements[i].setAttribute("empty", true)
		}
	}
}

function flashElements(elem, count)
{
	if(count==null)
	{
		count=0
	}
	if(count==0)
	{
		elem.setAttribute("_flash_classname", elem.className)
	}
	if(count<12)
	{
		elem.className = elem.getAttribute("_flash_classname")+"_flash_"+(count%2)
		setTimeout(function(){flashElements(elem, count+1)}, 150)
	}
	else
	{
		elem.className = elem.getAttribute("_flash_classname")
	}
}

function focusElement(elem)
{
	var parent = elem.parentNode;
	while(parent!=null && parent.scrollHeight-parent.offsetHeight==0 && parent.scrollWidth-parent.offsetWidth==0)
	{
		parent = parent.parentNode
	}
	if(parent == null)
	{
		return
	}
	if(parent.scrollTop < elem.offsetTop)
	{
		parent.scrollTop = elem.offsetTop + elem.offsetHeight - parent.offsetHeight + 5
	}
	else if(parent.scrollTop + parent.offsetHeight > elem.offsetTop + elem.offsetHeight)
	{
		parent.scrollTop = elem.offsetTop - 5
	}
	
	if(parent.scrollLeft < elem.offsetLeft)
	{
		parent.scrollLeft = elem.offsetLeft + elem.offsetWidth -  parent.offsetWidth + 5
	}
	else if(parent.scrollLeft + parent.offsetWidth > elem.offsetLeft + elem.offsetWidth)
	{
		parent.scrollLeft = elem.offsetLeft - 5
	}
}

function focusInput(elem)
{
	var inputs = elem.getElementsByTagName("INPUT")
	for(var i=0; i<inputs.length; i++)
	{
		if(inputs[i].type=="text" || inputs[i].type=="password")
		{
			if(inputs[i].getAttribute("readonly")) continue;
			try
			{
				inputs[i].focus();
				inputs[i].select();
				break;
			}
			catch(e){}
		}
	}
}

function setHtml(elem, html)
{
	elem.innerHTML = html;
	var inputs = elem.getElementsByTagName("INPUT")
	var theForm = null;
	for(var i=0; i<inputs.length; i++)
	{
		if(inputs[i].type.toUpperCase()=="CHECKBOX")
		{
			if(inputs[i].form != null)
			{
				theForm = inputs[i].form;
			}
		
			var div = document.createElement("DIV")
			div.input = inputs[i]
			inputs[i].parentNode.insertBefore(div, inputs[i])
			inputs[i].parentNode.insertBefore(inputs[i], div)
			if(inputs[i].getAttribute('values')==null)
			{
				if(inputs[i].getAttribute('value') == null)
				{
					inputs[i].setAttribute('values', 'on')
				}
				else
				{
					inputs[i].setAttribute('values', inputs[i].getAttribute('value'))
				}
			}

			if(inputs[i].checked)
			{
				var values = inputs[i].getAttribute('values').split(',')
				if(values.indexOf(inputs[i].value)<0)
				{
					inputs[i].setAttribute('_value', values[0])
				}
				else
				{
					inputs[i].setAttribute('_value', inputs[i].value)
				}
			}
			else
			{
				inputs[i].setAttribute('_value', '')
			}
			inputs[i].value = inputs[i].getAttribute('_value')
			inputs[i].checked = (inputs[i].value != '')
			if(inputs[i].checked)
			{
				inputs[i].setAttribute('_checked', true)
			}
			else
			{
				inputs[i].removeAttribute('_checked')
			}
			
			var tmp = inputs[i].parentNode.className
			inputs[i].parentNode.className = "-";
			inputs[i].parentNode.className = tmp;
			inputs[i].onclick = function()
			{
				var values = this.getAttribute('values').split(',')
				if(this.getAttribute('_value') == '')
				{
					this.setAttribute('_value', values[0])
				}
				else
				{
					var pos = values.indexOf(this.value)
					if(pos<0)
					{
						this.setAttribute('_value', inputs[i].values[0])
					}
					else if(pos==values.length-1)
					{
						this.setAttribute('_value', '')
					}
					else
					{
						this.setAttribute('_value', values[pos+1])
					}
				}
				
				this.value = this.getAttribute('_value')
				this.checked = (this.value != '')
				if(this.checked)
				{
					this.setAttribute('_checked', true)
				}
				else
				{
					this.removeAttribute('_checked')
				}
				this.parentNode.className+=""
			}
			div.onclick = function()
			{
				this.input.click()
			}
		}
	}
	if(theForm != null)
	{
		addEvent(theForm, "reset", function()
		{
			//use a timeout to fire the event AFTER the reset
			setTimeout(function(){
			for(var i=0; i<inputs.length; i++)
			{
				if(inputs[i].type.toUpperCase()=="CHECKBOX")
				{
					if(inputs[i].checked)
					{
						var values = inputs[i].getAttribute('values').split(',')
						if(values.indexOf(inputs[i].value)<0)
						{
							inputs[i].setAttribute('_value', values[0])
						}
						else
						{
							inputs[i].setAttribute('_value', inputs[i].value)
						}
					}
					else
					{
						inputs[i].setAttribute('_value', '')
					}
					inputs[i].value = inputs[i].getAttribute('_value')
					inputs[i].checked = (inputs[i].value != '')
					if(inputs[i].checked)
					{
						inputs[i].setAttribute('_checked', true)
					}
					else
					{
						inputs[i].removeAttribute('_checked')
					}
					
					var tmp = inputs[i].parentNode.className
					inputs[i].parentNode.className = "-";
					inputs[i].parentNode.className = tmp;
				}
			}}, 1)
		})
	}
}

function addEvent(elementObject, eventName, functionObject)
{
	if(document.addEventListener)
	{
		elementObject.addEventListener(eventName, function (evt)
		{
			functionObject(elementObject, evt)
		}, false);
	}
	else if(document.attachEvent)
	{
		elementObject.attachEvent("on" + eventName, function ()
		{
			functionObject(elementObject);
		})
	}
}


function getCookie(name)
{
	var cookies = document.cookie.split(';');
	for(var i = 0; i < cookies.length; i++)
	{
		var cookie = cookies[i].replace(/^\s+/, '');
		if(cookie.indexOf(name + '=') == 0)
		{
			return cookie.split("=")[1];
		}
	}
	return null;
}


if (window.HTMLElement)
{

    var element = HTMLElement.prototype;

    var capture = ["click",    "mousedown", "mouseup",    "mousemove", "mouseover", "mouseout" ];

    element.setCapture = function()
    {
        var self = this;
        var flag = false;
        this._capture = function(e)
        {
            if (flag) {return}
            flag = true;
            var event = document.createEvent("MouseEvents");
            event.initMouseEvent(e.type,
                e.bubbles, e.cancelable, e.view, e.detail,
                e.screenX, e.screenY, e.clientX, e.clientY,
                e.ctrlKey, e.altKey, e.shiftKey, e.metaKey,
                e.button, e.relatedTarget);
            self.dispatchEvent(event);
            flag = false;
        };
        for (var i=0; i<capture.length; i++)
        {
            window.addEventListener(capture[i], this._capture, true);
        }
    };

    element.releaseCapture = function()
    {
        for (var i=0; i<capture.length; i++)
        {
            window.removeEventListener(capture[i], this._capture, true);
        }
        this._capture = null;
    };

}


Array.prototype.indexOf = function(needle)
{
	for(var i=0; i<this.length; i++)
	{
		if(this[i]==needle)
		{
			return i
		}
	}
	return -1
}

document.onfocusout = function()
{
	if(window.event)
	{
		var target = window.event.srcElement
		target.removeAttribute("focused")
		if(target.tagName=="INPUT" && (target.type=="text" || target.type=="password"))
		{
			if(target.value=="")
			{
				target.setAttribute('empty', true)
			}
			else
			{
				target.removeAttribute("empty")
			}
		}
		if(target.parentNode != null)
		{
			target.parentNode.className+=""
		}
	}
}
document.onfocusin = function()
{
	if(window.event)
	{
		window.event.srcElement.setAttribute("focused", true)
		window.event.srcElement.parentNode.className+=""
	}
}

document.onchange = function(e)
{
	if(window.event)
	{
		var target = window.event.srcElement
	}
	else
	{
		var target = e.target
	}
	if(target.tagName!="INPUT")
	{
		return
	}
	if(target.type=="text" || target.type=="password")
	{
		if(target.value=="")
		{
			target.setAttribute('empty', true)
		}
		else
		{
			target.removeAttribute("empty")
		}
		target.className+=""
	}
}


/**
 * Copyright (c)2005-2007 Matt Kruse (javascripttoolbox.com)
 * 
 * Dual licensed under the MIT and GPL licenses. 
 * This basically means you can use this code however you want for
 * free, but don't claim to have written it yourself!
 * Donations always accepted: http://www.JavascriptToolbox.com/donate/
 * 
 * Please do not link to the .js files on javascripttoolbox.com from
 * your site. Copy the files locally to your server instead.
 * 
 */
var Position = (function() {
  // Resolve a string identifier to an object
  // ========================================
  function resolveObject(s) {
    if (document.getElementById && document.getElementById(s)!=null) {
      return document.getElementById(s);
    }
    else if (document.all && document.all[s]!=null) {
      return document.all[s];
    }
    else if (document.anchors && document.anchors.length && document.anchors.length>0 && document.anchors[0].x) {
      for (var i=0; i<document.anchors.length; i++) {
        if (document.anchors[i].name==s) { 
          return document.anchors[i]
        }
      }
    }
  }
  
  var pos = {};
  pos.$VERSION = 1.0;
  
  // Set the position of an object
  // =============================
  pos.set = function(o,left,top) {
    if (typeof(o)=="string") {
      o = resolveObject(o);
    }
    if (o==null || !o.style) {
      return false;
    }
    
    // If the second parameter is an object, it is assumed to be the result of getPosition()
    if (typeof(left)=="object") {
      var pos = left;
      left = pos.left;
      top = pos.top;
    }
    
    o.style.left = left + "px";
    o.style.top = top + "px";
    return true;
  };
  
  // Retrieve the position and size of an object
  // ===========================================
  pos.get = function(o) {
    var fixBrowserQuirks = true;
      // If a string is passed in instead of an object ref, resolve it
    if (typeof(o)=="string") {
      o = resolveObject(o);
    }
    
    if (o==null) {
      return null;
    }
    
    var left = 0;
    var top = 0;
    var width = 0;
    var height = 0;
    var parentNode = null;
    var offsetParent = null;
  
    
    offsetParent = o.offsetParent;
    var originalObject = o;
    var el = o; // "el" will be nodes as we walk up, "o" will be saved for offsetParent references
    while (el.parentNode!=null) {
      el = el.parentNode;
      if (el.offsetParent==null) {
      }
      else {
        var considerScroll = true;
        /*
        In Opera, if parentNode of the first object is scrollable, then offsetLeft/offsetTop already 
        take its scroll position into account. If elements further up the chain are scrollable, their 
        scroll offsets still need to be added in. And for some reason, TR nodes have a scrolltop value
        which must be ignored.
        */
        if (fixBrowserQuirks && window.opera) {
          if (el==originalObject.parentNode || el.nodeName=="TR") {
            considerScroll = false;
          }
        }
        if (considerScroll) {
          if (el.scrollTop && el.scrollTop>0) {
            top -= el.scrollTop;
          }
          if (el.scrollLeft && el.scrollLeft>0) {
            left -= el.scrollLeft;
          }
        }
      }
      // If this node is also the offsetParent, add on the offsets and reset to the new offsetParent
      if (el == offsetParent) {
        left += o.offsetLeft;
        if (el.clientLeft && el.nodeName!="TABLE") { 
          left += el.clientLeft;
        }
        top += o.offsetTop;
        if (el.clientTop && el.nodeName!="TABLE") {
          top += el.clientTop;
        }
        o = el;
        if (o.offsetParent==null) {
          if (o.offsetLeft) {
            left += o.offsetLeft;
          }
          if (o.offsetTop) {
            top += o.offsetTop;
          }
        }
        offsetParent = o.offsetParent;
      }
    }
    
  
    if (originalObject.offsetWidth) {
      width = originalObject.offsetWidth;
    }
    if (originalObject.offsetHeight) {
      height = originalObject.offsetHeight;
    }
    
    return {'left':left, 'top':top, 'width':width, 'height':height
        };
  };
  
  // Retrieve the position of an object's center point
  // =================================================
  pos.getCenter = function(o) {
    var c = this.get(o);
    if (c==null) { return null; }
    c.left = c.left + (c.width/2);
    c.top = c.top + (c.height/2);
    return c;
  };
  
  return pos;
})();
